package org.pictual.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.pictual.business.dao.AccesoBD;
import org.pictual.business.vo.CompletoVO;
import org.pictual.controller.mainPackage.Language;
import org.pictual.service.PictualDelegateFactory;
import org.pictual.util.Cache;
import org.pictual.util.PalabraEnTabla;
import org.pictual.util.SessionKeys;

public class CacheAction extends Action{

	protected static Logger log= Logger.getLogger(CacheAction.class);
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		HttpSession sesion= request.getSession();
		ServletContext context = getServlet().getServletContext();
		List<PalabraEnTabla> listadoPalabras = new ArrayList<PalabraEnTabla>();
				
		List<CompletoVO> listado = new ArrayList<CompletoVO>();
		AccesoBD accesoBD = new AccesoBD();
		//CACHE
		Cache <String, CompletoVO> cache = (Cache<String, CompletoVO>) context.getAttribute("cache");
		
		if(cache == null){
			log.info("CACHE: No existe cache. Se crea");
			cache = new Cache<String, CompletoVO>(100);
			context.setAttribute("cache", cache);
			
			
			if(accesoBD.conecta()){
				//Se rellena cache
				log.info("Se rellena cache con las mas visitadas");
				String cadena;
				for(CompletoVO palabra: PictualDelegateFactory.getIdiomaDelegate().selectlistadoMasVisitadas(50)){
					cadena=palabra.getPalabra()+
							"/"+Language.resolve(palabra.getIdioma())+
							"/"+Language.resolve(palabra.getWiki());
					cache.put(cadena, palabra);
				}
			}else{
				log.error("no se ha podido rellenar la cache");
			}

		}
		String cadenaParaCache;
		Iterator it = cache.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry e = (Map.Entry)it.next();
//			System.out.println(e.getKey() + " " + e.getValue());
			
			cadenaParaCache = (String) e.getKey();
			log.info("CACHE: Clave de cache: "+cadenaParaCache);
			
			CompletoVO palabraEnCache = cache.get(cadenaParaCache);
			
			listado.add(palabraEnCache);
			
		}
		PalabraEnTabla pet;
		boolean haveImage;
		
		for(CompletoVO completo: listado){
			
//			haveImage = PictualDelegateFactory.getPalabraDelegate().haveImages(completo.getId());
			
			pet = new PalabraEnTabla(
					completo.getPalabra(),
					Language.resolve(completo.getIdioma()), 
					Language.resolve(completo.getWiki()), 
					false, 
					completo.getId().intValue());
			
			listadoPalabras.add(pet);
			
		}
		
		sesion.setAttribute(SessionKeys.WORDS, listadoPalabras);
		
		return mapping.findForward("success");
	}
}
